/*
 * MyGWT User Extensions
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.mygwt.ux.samples.explorer.client;

import java.util.ArrayList;
import java.util.List;

import net.mygwt.ui.client.Events;
import net.mygwt.ui.client.Style;
import net.mygwt.ui.client.event.BaseEvent;
import net.mygwt.ui.client.event.Listener;
import net.mygwt.ui.client.event.SelectionListener;
import net.mygwt.ui.client.util.TreeBuilder;
import net.mygwt.ui.client.widget.ContentPanel;
import net.mygwt.ui.client.widget.ThemeSelector;
import net.mygwt.ui.client.widget.ToolButton;
import net.mygwt.ui.client.widget.Viewport;
import net.mygwt.ui.client.widget.WidgetContainer;
import net.mygwt.ui.client.widget.layout.BorderLayout;
import net.mygwt.ui.client.widget.layout.BorderLayoutData;
import net.mygwt.ui.client.widget.layout.FillLayout;
import net.mygwt.ui.client.widget.layout.RowData;
import net.mygwt.ui.client.widget.layout.RowLayout;
import net.mygwt.ui.client.widget.tree.Tree;
import net.mygwt.ui.client.widget.tree.TreeItem;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;

/**
 * 
 * @author darrell
 *
 */
public class Explorer implements EntryPoint {

  private Viewport viewport;

  private ContentPanel westPanel;
  private ContentPanel centerPanel;

  private Tree tree;
  private TreeItem current;

  private Page currentPage;
  private List pages = new ArrayList();

  public Explorer() {
    pages.add(new TestPage());
    pages.add(new TextBoxPage());
    pages.add(new SliderPage());
    pages.add(new SimpleWizardPage());
    pages.add(new EditTablePage());
    pages.add(new InputPage());
    pages.add(new ActionsPage());
  }

  private void show(TreeItem item) {
    if (current == item) {
      return;
    }
    current = item;
    currentPage = null;
    String pageId = item.getId();
    for (int i = 0; i < pages.size(); i++) {
      Page page = (Page) pages.get(i);
      if (page.getId().equals(pageId)) {
        currentPage = page;
        break;
      }
    }

    StringBuffer sb = new StringBuffer();
    while (!item.isRoot()) {
      sb.insert(0, " / " + item.getText());
      item = item.getParentItem();
    }
    centerPanel.setText(sb.substring(3));

    if (currentPage == null) {
      centerPanel.removeAll();
      centerPanel.layout(true);
      return;
    }

    if (!currentPage.initialized) {
      currentPage.init();
    }

    centerPanel.removeAll();
    centerPanel.add(currentPage);
    centerPanel.layout(true);
  }

  public void onModuleLoad() {
    viewport = new Viewport();
    viewport.setStyleName("my-border-layout");
    
    viewport.setLayout(new RowLayout());
    viewport.setBorders(false);
    
    HorizontalPanel header = new HorizontalPanel();
    header.setStyleName("header");
    Label lbl = new Label("MyGWT UX Explorer Demo");
    lbl.setStyleName("title");
    header.add(lbl);
    
    WidgetContainer main = new WidgetContainer();
    main.setLayout(new BorderLayout());

    // create the tree contents
    tree = new Tree(Style.SINGLE);
    tree.setItemImageStyle("icon-list");

    // wrap tree to allow scrolling in content panel
    WidgetContainer ct = new WidgetContainer();
    ct.setScrollEnabled(true);
    ct.add(tree);

    Element toc = DOM.getElementById("toc");
    TreeBuilder.buildTree(tree, toc);
    tree.addListener(Events.SelectionChange, new Listener() {
      public void handleEvent(BaseEvent be) {
        TreeItem item = (TreeItem) be.item;
        show(item);
      }
    });

    BorderLayoutData westData = new BorderLayoutData(Style.WEST, 200, 150, 300);
    westPanel = new ContentPanel(Style.HEADER);
    westPanel.setLayout(new FillLayout());
    westPanel.setText("Navigation");

    ToolButton expandAll = new ToolButton("my-tool-plus");
    expandAll.setToolTip("Expand All");
    expandAll.addSelectionListener(new SelectionListener() {
      public void widgetSelected(BaseEvent be) {
        tree.expandAll();
      }
    });
    westPanel.getHeader().addWidget(expandAll);

    ToolButton collapseAll = new ToolButton("my-tool-minus");
    collapseAll.setToolTip("Collapse All");
    collapseAll.addSelectionListener(new SelectionListener() {
      public void widgetSelected(BaseEvent be) {
        tree.collapseAll();
      }
    });
    westPanel.getHeader().addWidget(collapseAll);
    westPanel.add(ct);

    centerPanel = new ContentPanel(Style.HEADER);
    centerPanel.getHeader().addWidget(new ThemeSelector());
    centerPanel.setLayout(new FillLayout());

    main.add(westPanel, westData);
    main.add(centerPanel, new BorderLayoutData(Style.CENTER));

    viewport.add(header, new RowData(RowData.FILL_HORIZONTAL));
    viewport.add(main, new RowData(RowData.FILL_BOTH));
    viewport.layout();

    viewport.hideLoadingPanel("loading");

  }

}

